/*
 *	Copyright 2011 andro-inject developers 
 *	Licensed under the Apache License, Version 2.0 (the "License"); 
 *	you may not use this file except in compliance with the License. 
 *	You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 *	Unless required by applicable law or agreed to in writing, software 
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License. 
 * 
*/

package com.androinject.core.logging.console;

import com.androinject.core.logging.Logger;
import com.androinject.core.logging.Logging;

public class ConsoleLogger implements Logger {
	
	private Class<?> loggedClass;
	private int logLevel;
	
	
	public ConsoleLogger(Class<?> loggedClass, int logLevel) {
		this.loggedClass = loggedClass;
		this.logLevel = logLevel;
	}
	

	@Override
	public void trace(String message) {
		this.log(Logging.LEVEL_TRACE, "TRACE", message);
	}

	
	@Override
	public void debug(String message) {
		this.log(Logging.LEVEL_DEBUG, "DEBUG", message);		
	}

	
	@Override
	public void info(String message) {
		this.log(Logging.LEVEL_INFO, "INFO ", message);		
	}
	

	@Override
	public void warn(String message) {
		this.log(Logging.LEVEL_WARN, "WARN ", message);
	}
	

	@Override
	public void error(String message, Throwable cause) {
		this.log(Logging.LEVEL_ERROR, "ERROR", message);
		if (cause != null) {
			cause.printStackTrace();
		}
	}
	
	
	@Override
	public void error(String message) {
		this.error(message, null);
	}

	
	@Override
	public void fatal(String message, Throwable cause) {
		this.log(Logging.LEVEL_FATAL, "FATAL", message);
		if (cause != null) {
			cause.printStackTrace();
		}
	}
	
	
	private void log(int level, String prefix, String message) {
		if (level >= this.logLevel) {
			System.out.println(prefix + " " + loggedClass.getName() + ":   " + message);
		}
	}

}
